// ViewhistoryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "clipmon.h"
#include "ViewhistoryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern "C" DWORD __stdcall depackfile(char *filein, char *fileout);
extern "C" void mycallback(UINT,UINT);

extern CString g_exedir;

extern void PackOld();


/////////////////////////////////////////////////////////////////////////////
// CViewhistoryDlg dialog


CViewhistoryDlg::CViewhistoryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CViewhistoryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CViewhistoryDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_initDate = COleDateTime::GetCurrentTime();
}

CViewhistoryDlg::~CViewhistoryDlg()
{
// 	CString s;s.Format("%d", m_initDate.GetDayOfYear());AfxMessageBox(s);
	if (COleDateTime::GetCurrentTime().GetDayOfYear() - m_initDate.GetDayOfYear() > 0)
	{
		PackOld();
	}
	else
	{
		BOOL bExistHistory;
		CString fName, fName2;
		CFileFind myFind ;
		bExistHistory = myFind.FindFile(g_exedir+"history\\*.rtf",0);
		while(bExistHistory){
			int result = myFind.FindNextFile() ;
			
			fName = myFind.GetFileName() ;
			if (fName != COleDateTime::GetCurrentTime().Format("%y%m%d.rtf"))
			{
				::DeleteFile(g_exedir+"history\\" + fName);
			}
			
			if( result==0 )
				break ;
		}
	}
}


void CViewhistoryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CViewhistoryDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CViewhistoryDlg, CDialog)
	//{{AFX_MSG_MAP(CViewhistoryDlg)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewhistoryDlg message handlers

void CViewhistoryDlg::OnOK() 
{
	// TODO: Add extra validation here
	
// 	CDialog::OnOK();
}

BOOL CViewhistoryDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_list.InsertColumn(0,_T("Date"),LVCFMT_CENTER,60);
	m_list.DeleteAllItems();

	BOOL bExistHistory;
	int iItemIndex = 0 ;
	CString fName, fName2;
	CFileFind myFind ;
	bExistHistory = myFind.FindFile(g_exedir+"history\\*.lz",0);
	while(bExistHistory){
		int result = myFind.FindNextFile() ;
		fName = myFind.GetFileName() ;
		fName = fName.Left(fName.ReverseFind('.'));
		m_list.InsertItem(iItemIndex++, fName);

// 		fName2 = fName.Left(fName.ReverseFind('.'));
// 		fName2 += ".rtf";
		
		// pack the file
// 		if (!depackfile((g_exedir + "history\\" + fName).GetBuffer(MAX_PATH),
// 			(g_exedir + "history\\" + fName2).GetBuffer(MAX_PATH)))
// 		{
// 			::DeleteFile(g_exedir + "history\\" + fName);
// 		}
// 		if (depackfile((g_exedir + "history\\090224.lz").GetBuffer(MAX_PATH),
// 			(g_exedir + "history\\090224.rtf").GetBuffer(MAX_PATH)))
// 		{
// 			AfxMessageBox("fail!");
// 		}
		
		if( result==0 )
			break ;
	}

	CFileFind myFindrtf;
	fName = g_exedir + COleDateTime::GetCurrentTime().Format("history\\%y%m%d.rtf");
	if (myFind.FindFile(fName,0))
	{
		m_list.InsertItem(iItemIndex++, 
			fName.Mid(fName.ReverseFind('\\')+1, fName.ReverseFind('.') - fName.ReverseFind('\\') - 1));
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CViewhistoryDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CString fName, fName2;

	POSITION pos = m_list.GetFirstSelectedItemPosition();
	int itemIndex = m_list.GetNextSelectedItem(pos) ;

	fName = m_list.GetItemText(itemIndex, 0);

	if (fName == COleDateTime::GetCurrentTime().Format("%y%m%d"))
	{
		CString path;
		path.Format("\"%s\"", g_exedir + COleDateTime::GetCurrentTime().Format("history\\%y%m%d.rtf"));
		ShellExecute(this->m_hWnd, "open", "wordpad.exe", path,NULL,SW_NORMAL);
	}
	else if (!depackfile((g_exedir + "history\\" + fName + ".lz").GetBuffer(MAX_PATH),
		(g_exedir + "history\\" + fName + ".rtf").GetBuffer(MAX_PATH)))
	{
// 		::DeleteFile(g_exedir + "history\\" + fName);
		CString path;
		path.Format("\"%s\"", g_exedir + "history\\" + fName + ".rtf");
		ShellExecute(this->m_hWnd, "open", "wordpad.exe", path,NULL,SW_NORMAL);
	}

	
	*pResult = 0;
}
